# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=shiboken
pkgbase=mingw-w64-${_realname}-qt4
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt4 ${MINGW_PACKAGE_PREFIX}-python{2,3}-${_realname}-qt4)
pkgver=1.2.2
pkgrel=4
arch=('any')
url="https://pyside.github.io/docs/pyside/"
license=("LGPL")
install=${_realname}-${CARCH}.install
source=(https://pypi.python.org/packages/source/P/PySide/PySide-${pkgver}.tar.gz
        shiboken.patch
        shiboken-python-3.5-support.patch)
sha256sums=('53129fd85e133ef630144c0598d25c451eab72019cdcb1012f2aec773a3f25be'
            '603b3ac935a60494cc9cb52c3e0847f61353df546dfea88544025e9ddea5e7c0'
            '8afd69cca296fb0479a24e7ff3f147a3ce968e99e3dfef408ff5eafc3ee4a194')
makedepends=("${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-qt4"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/PySide-${pkgver}
  patch -p1 -i ${srcdir}/shiboken.patch
  patch -p1 -i ${srcdir}/shiboken-python-3.5-support.patch
}

build() {
  for pyv in python2 python3; do
    local _conf=
    if [ "${pyv}" = "python3" ]; then
      _conf="-DUSE_PYTHON3=yes"
    fi
    [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
    cp -rf PySide-${pkgver} build-${MINGW_CHOST}-${pyv}
    pushd ${srcdir}/build-${MINGW_CHOST}-${pyv}/sources/shiboken > /dev/null

    export MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX"
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${_conf} \
      -DCMAKE_BUILD_TYPE=RELEASE \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/${pyv}.exe \

    make

    popd > /dev/null
  done

}

fix_paths() {
  local prefixwin=$(cygpath -am ${MINGW_PREFIX})
  local prefixinst=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  local _files=$(find ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver} -type f -iname "*.cmake" -o -iname "*.pc")
  for f in ${_files}; do
    plain "Patching ${f}..."
    sed -e "s|${prefixwin}|${MINGW_PREFIX}|g" \
        -e "s|${prefixinst}|${MINGW_PREFIX}|g" \
        -i ${f}
  done
}

package_shiboken() {
  pkgdesc="CPython bindings generator for C++ libraries (mingw-w64)"
  makedepends=("${MINGW_PACKAGE_PREFIX}-libxml2"
               "${MINGW_PACKAGE_PREFIX}-libxslt"
               "${MINGW_PACKAGE_PREFIX}-python3"
               "${MINGW_PACKAGE_PREFIX}-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python3/sources/shiboken
  make DESTDIR=${pkgdir} install

  cd ${srcdir}/build-${MINGW_CHOST}-python2/sources/shiboken/data
  #install -Dm644 ShibokenConfig-python2.7.cmake ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/
  #install -Dm644 shiboken.pc ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Shiboken-py2.pc

  rm ${pkgdir}${MINGW_PREFIX}/bin/*.dll
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/python*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/libshiboken*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/ShibokenConfig*python*.cmake

  fix_paths
}

package_python2-shiboken() {
  pkgdesc="Support library for Python2 bindings (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-libxslt"
           "${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-shiboken-qt4"
           "${MINGW_PACKAGE_PREFIX}-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python2/sources/shiboken
  make DESTDIR=${pkgdir} install

  cd data
  install -Dm644 ShibokenConfig-python2.7.cmake ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/
  mv ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/shiboken{,-py2}.pc

  rm ${pkgdir}${MINGW_PREFIX}/bin/*.exe
  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/ShibokenConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/ShibokenConfig.cmake

  fix_paths
}

package_python3-shiboken() {
  pkgdesc="Support library for Python3 bindings (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-libxslt"
           "${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-shiboken-qt4"
           "${MINGW_PACKAGE_PREFIX}-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python3/sources/shiboken
  make DESTDIR=${pkgdir} install

  rm ${pkgdir}${MINGW_PREFIX}/bin/*.exe
  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/ShibokenConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken-${pkgver}/ShibokenConfig.cmake

  fix_paths
}

# Wrappers
package_mingw-w64-i686-shiboken-qt4() {
  package_shiboken
}

package_mingw-w64-i686-python2-shiboken-qt4() {
  package_python2-shiboken
}

package_mingw-w64-i686-python3-shiboken-qt4() {
  package_python3-shiboken
}

package_mingw-w64-x86_64-shiboken-qt4() {
  package_shiboken
}

package_mingw-w64-x86_64-python2-shiboken-qt4() {
  package_python2-shiboken
}

package_mingw-w64-x86_64-python3-shiboken-qt4() {
  package_python3-shiboken
}
